# -*- coding: utf-8 -*-
__author__ = 'xujianhua'
import numpy as np
import cv2
from cv2 import *
import Queue
import multiprocessing
myqueue = Queue.Queue(maxsize = 100)
def work():
    cap = cv2.VideoCapture(0)
    #cap.set(cv2.CAP_PROP_FRAME_WIDTH,1280)
    #cap.set(cv2.CAP_PROP_FRAME_HEIGHT,720)
    #cap.set(cv2.CAP_PROP_FPS,20)
    frame_val=1
    m=0
    n=0
    while(True):
        # Capture frame-by-frame
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
        
        #kernel = np.ones((3,3),np.float32)/9
        #filter_frame = cv2.filter2D(gray,-1,kernel)
        # Display the resulting frame
        #result_arry = np.ones((width,height,3),np.uint8,1)
        # resulqt = cv2.cv.CreateMat(480,640,cv2.CV_16UC3)
        # src = cv2.cv.fromarray(hsv)
        # cv2.cv.Convert(src,result)
        #result =  cv2.cvtColor(hsv, cv2.COLOR_HSV2BGR)
    
        #mylist = [[result[i,j] for i in range(width)] for j in range(height)]
        #result_frame = np.array(mylist)
        #cv2.imshow('frame',result)
    
    
        kernel = np.ones((5,5),np.float32)/25
        filter_frame = cv2.medianBlur(gray,9)#cv2.filter2D(dst_img,-1,kernel)  #41
        #cv2.imshow('filter_frame',filter_frame)
        hist = np.average(filter_frame.ravel())
        _,out=cv2.threshold(filter_frame,230,255,cv2.THRESH_BINARY)
        cv2.imshow('out1',out)
        # # cv2.imshow('out1',out)
        contours, hierarchy = cv2.findContours(out,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
        #print "------------------------------------------contours-----------------------------------------------\n",(contours)
        #cv2.drawContours(frame,contours,-1,(0,255,0),-1)
        #cv2.imshow('out',frame)
        # # print "out",out.shape
    
        #print "out",out
        # #fs = open('data.txt', 'w')
        # #S='\n'.join(str(num)[1:-1] for num in filter_frame)
        # #fs.write(S)
        # #fs.close()
        sumx=0
        sumy=0
        area=0
        max_area = 0
        max_x = 0
        max_y = 0
        cnt = 0
        a=0
        r = (0,0)
        max_index = 0
        for i in range(0,len(contours)):
            area = cv2.contourArea(contours[i])
            if area >  max_area:
                center,r = cv2.minEnclosingCircle(contours[i])
                max_area = area
                max_index = i
            #print 'center,r',center,r 
            
        if max_area != 0:
            cv2.drawContours(frame,contours,max_index,255)
            #print "r:\r\n",r
            #cv2.circle(frame,(int(center[0]),int(center[1])),int(r),255,-1)
            #print "x-y",(sumy/area,sumx/area)
            
            cnt = contours[max_index]
            mask = np.zeros(filter_frame.shape,np.uint8)
            cv2.drawContours(mask,[cnt],0,255,-1)
            mean = cv2.mean(filter_frame,mask = mask)
            s=mean[0]
            pixelpoints = np.transpose(np.nonzero(mask))
            a =s*len(pixelpoints)
        #print a
        
        #cv2.imshow('filter_frame',filter_frame)
        #cv2.imshow('result frame',result)
        mean_val = cv2.mean(filter_frame)
        t= mean_val[0]*filter_frame.size
        #print filter_frame.size
        #print  t
        d= 0.0
        d= "%.5f" % (a/t)
        #print "1帧"
        #print d
        m+=t
        n+=a
        frame_val+=1
        if cv2.waitKey(1) & 0xFF == ord('q'):
            cap.release()
            cv2.destroyAllWindows()
            break
        if frame_val%20==0:
            #print "20帧"
            #print "%.5f" % (n/m)
            return   int((n/m)*10000)
        
        
            #print  (a/t)

        cv2.imshow('image',frame)

if __name__ == '__main__':
    while(1):
    	#work()
    	print work()

    
# When everything done, release the capture